package com.hf.order

import com.hf.constant.OrderLunchConst
import com.hf.date.DateUtils
import com.hf.security.OrderUser

class UserOrderHistoryService {

	List<UserOrderHistory> getUserOrderHistoryListPeriod(OrderUser user, List<Date>  dates) {
		List<UserOrderHistory> userOrderHistoryList = UserOrderHistory.findAllByUserAndOrderDateBetween(user, dates[0], dates[1])
		userOrderHistoryList.sort() { a,b ->
			a.orderDate <=> b.orderDate
		}

		return userOrderHistoryList
	}

	List<OrderUser> getOrderedUserListToay() {
		Date currentDay = DateUtils.getToday()

		List l = UserOrderHistory.executeQuery("select urh.user, count(*) from UserOrderHistory urh where urh.orderDate = :orderDate group by urh.user having count(*) > 1", [orderDate: DateUtils.today])

		if (!l.empty) {
			log.info "Many users order more than one times today: $l"
			throw new Exception()
		}

		List<UserOrderHistory> userOrderHistoryList = UserOrderHistory.findAllByOrderDate(currentDay)
		List<OrderUser> orderedUserList = userOrderHistoryList.collect { UserOrderHistory userOrderHistory -> userOrderHistory.user }

		return orderedUserList
	}

	synchronized Map saveUserOrderHistory(OrderUser user) {
		Map resultMap = [:]
		String message

		Date currentDay = DateUtils.getToday()
		UserOrderHistory userOrderHistory = getUserOrderHistoryToday(user)
		if (new Date() < DateUtils.getFetchEmailClocToday()) {
			if (userOrderHistory) {
				message = "Sorry! You have already ordered."
			} else {
				userOrderHistory = new UserOrderHistory(user : user, orderDate : currentDay)
				userOrderHistory.save()

				message = "You ordered successfully !"
				resultMap['success'] = true
			}
		} else {
			message = "Sorry! Can't operate after $OrderLunchConst.FETCH_EMAIL_CLOCK o'clock, ask admin for help. "
		}
		resultMap['message'] = message
		return resultMap
	}

	synchronized Map deleteUserOrderHistory(OrderUser user) {
		Map resultMap = [:]
		String message

		UserOrderHistory userOrderHistory = getUserOrderHistoryToday(user)
		if (new Date() < DateUtils.getFetchEmailClocToday()) {
			if (userOrderHistory) {
				userOrderHistory.delete()
				message = "You canceled successfully !"
				resultMap['success'] = true
			} else {
				message = "Sorry! You have not ordered."
			}
		} else {
			message = "Sorry! Can't operate after $OrderLunchConst.FETCH_EMAIL_CLOCK o'clock, ask admin for help."
		}
		resultMap['message'] = message
		return resultMap
	}

	UserOrderHistory getUserOrderHistoryToday(OrderUser user) {
		Date currentDay = DateUtils.getToday()
		UserOrderHistory userOrderHistory = UserOrderHistory.findByUserAndOrderDate(user, currentDay)

		return userOrderHistory
	}

	boolean getUserOrderHistoryStatus(OrderUser user) {
		UserOrderHistory userOrderHistory = getUserOrderHistoryToday(user)
		if (userOrderHistory) {
			return true
		} else {
			return false
		}
	}

	List<UserOrderHistory> getUserOrderHistortyListPeriod(List<Date> periodStartEndDay) {
		List<UserOrderHistory> userOrderHistoryList = UserOrderHistory.findAllByOrderDateBetween(periodStartEndDay[0], periodStartEndDay[1])
		return userOrderHistoryList
	}

	Map<OrderUser, List<UserOrderHistory>> getUserOrderHistoryOfPeriodMap(List<Date> periodStartEndDay) {
		Map<OrderUser, List<UserOrderHistory>> userOrderHistoryMap = [:]
		getUserOrderHistortyListPeriod(periodStartEndDay).each { UserOrderHistory userOrderHistory ->
			List<UserOrderHistory> userOrderHistoryList = userOrderHistoryMap.get(userOrderHistory.user)
			if (!userOrderHistoryList) {
				userOrderHistoryList = []
				userOrderHistoryMap.put(userOrderHistory.user, userOrderHistoryList)
			}
			userOrderHistoryList << userOrderHistory
		}

		return userOrderHistoryMap
	}

	// Month Stats
	int getOrderMonthCount(List<Date> periodStartEndDay) {
		int orderMonthCount = UserOrderHistory.countByOrderDateBetween(periodStartEndDay[0], periodStartEndDay[1])

		return orderMonthCount
	}

	List getOrderMonthCountList(List<Date> periodStartEndDay) {
		List orderCountOfMonthList = UserOrderHistory.executeQuery("""\
					select urh.orderDate, count(*) \
					from UserOrderHistory urh \
					where urh.orderDate between :orderDateStart and :orderDateEnd \
					group by urh.orderDate \
					order by urh.orderDate""",
					[orderDateStart : periodStartEndDay[0], orderDateEnd : periodStartEndDay[1]])

		return orderCountOfMonthList
	}

	// User Period Stats
	List getOrderUserPeriodCountList(List<Date> periodStartEndDay) {
		List orderCountOfMonthList = UserOrderHistory.executeQuery("""\
					select urh.user, count(*) \
					from UserOrderHistory urh \
					where urh.orderDate between :orderDateStart and :orderDateEnd \
					group by urh.user""",
			[orderDateStart : periodStartEndDay[0], orderDateEnd : periodStartEndDay[1]])

			return orderCountOfMonthList
		
	}
}


